package com.zggk.lzz.sjcx;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zggk.lzz.R;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.sjcx.deal.DealFragment;
import com.zggk.lzz.sjcx.done.DoneFragment;
import com.zggk.lzz.sx.SxActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class SjcxActivity extends MVPBaseActivity<SjcxContract.View, SjcxPresenter> implements SjcxContract.View {


    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.now_showing)
    RadioButton nowShowing;
    @Bind(R.id.next_showing)
    RadioButton nextShowing;
    @Bind(R.id.radio_layout)
    RadioGroup radioLayout;
    @Bind(R.id.hedler_layout)
    RelativeLayout hedlerLayout;
    @Bind(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @Bind(R.id.disease_new_parent_layout)
    RelativeLayout diseaseNewParentLayout;
    private DealFragment dealFragment;
    private DoneFragment doneFragment;

    private int TYPE;

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.act_sjcx;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initView();

    }

    private void initView() {
        setTitleText("事件查询");
        goBack();
        cancleButton.setVisibility(View.VISIBLE);
        cancleButton.setText("筛选");
        dealFragment = new DealFragment();
        doneFragment = new DoneFragment();
        goToFragment(dealFragment);
    }

    Fragment mCurrentFragment;

    public void goToFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!fragment.isAdded()) { // 先判断是否被add过
            if (mCurrentFragment != null) {
                transaction.hide(mCurrentFragment).add(R.id.fragment_container, fragment).commitAllowingStateLoss();
            } else {
                transaction.add(R.id.fragment_container, fragment).commitAllowingStateLoss();
            }
        } else {
            if (mCurrentFragment != null) {
                transaction.hide(mCurrentFragment).show(fragment).commitAllowingStateLoss();
            } else {
                transaction.show(fragment).commitAllowingStateLoss();
            }
        }
        mCurrentFragment = fragment;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            if (requestCode == 1) {
                String orgName = data.getStringExtra("orgName");
                String etype = data.getStringExtra("etype");
                String startDate = data.getStringExtra("startDate");
                String endDate = data.getStringExtra("endDate");
                if (TYPE == 1) {
                    dealFragment.refreshDataMethod(orgName, etype, startDate, endDate);
                } else {
                    doneFragment.refreshDataMethod(orgName, etype, startDate, endDate);
                }
            }
        }
    }


    @OnClick({R.id.now_showing, R.id.next_showing, R.id.cancle_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancle_button:
                Intent intent2 = new Intent(SjcxActivity.this, SxActivity.class);
                startActivityForResult(intent2, 1);
                break;
            case R.id.now_showing:
                goToFragment(dealFragment);
                nowShowing.setTextColor(getResources().getColor(R.color.white));
                nextShowing.setTextColor(getResources().getColor(R.color.theme_color));
                break;
            case R.id.next_showing:
                goToFragment(doneFragment);
                nowShowing.setTextColor(getResources().getColor(R.color.theme_color));
                nextShowing.setTextColor(getResources().getColor(R.color.white));
                break;
        }
    }
}
